package com.vtest.quartz.task.report;

import com.vtest.quartz.domain.vtest.report.PRALYHDHoldWeekEntity;
import com.vtest.quartz.service.vtest.report.CommonReportService;
import com.vtest.quartz.service.vtest.report.PRAService;
import com.vtest.quartz.utils.MailHelper;
import com.vtest.quartz.utils.excel.ExcelUtil;
import com.vtest.quartz.utils.others.FileUtils;
import com.vtest.quartz.utils.others.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Directory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @program: Quartz
 * @author: tiger
 * @create: 2025-01-02 14:00
 * @Description :
 */

@Slf4j
@Component("PRAReport")
public class PRAReport {

    @Autowired
    private PRAService praService;

    /*
     * PRA LYHD HOLD周报  每周一7点
     */
    public void sendLYHDHoldWeekReport() throws IOException {
        log.debug("[PRA LYHD HOLD 周报表] 任务开始执行");
        List<PRALYHDHoldWeekEntity> holdInfoList = praService.getHoldInfo();

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String fileName = "PRA Hold lot by weekly_" + now.format(formatter);


        ExcelUtil<PRALYHDHoldWeekEntity> util = new ExcelUtil<>(PRALYHDHoldWeekEntity.class);
        util.exportExcel(holdInfoList, fileName, StringUtils.EMPTY, true);

        String remoteFilePath = "\\\\195.170.13.161\\Files\\PRAHold\\";
        if (!new File(remoteFilePath).exists()) new File(remoteFilePath).mkdir();
        String remoteFile = remoteFilePath + fileName + ".xlsx";


        File localFile = new File(util.downFile);
        if (localFile.exists()) {
            MailHelper helper = new MailHelper();
            helper.sendMail("ptenjp1@v-test.com.cn"
                    , "ruhu.wang@v-test.com.cn", "PRA LYHD HOLD 周报表"
                    , "PRA LYHD HOLD 周报表", util.downFile);
        }

        Files.move(Paths.get(util.downFile), Paths.get(remoteFile));
    }
}
